﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ABELGX
{
    public class ButtonVM
    {
        public RegisterBool RegisterCmd { get; }
        public RegisterBool RegisterInfo { get; }
        public ButtonVMType Type {  get;}
        public ButtonVM(Button button, ButtonVMType type, RegisterBool regCmd, RegisterBool regInfo) 
        {
            Type = type;
            RegisterCmd = regCmd;
            RegisterInfo = regInfo;
            button.Visibility = regInfo.Visiable ? Visibility.Visible : Visibility.Hidden;
            if (!regInfo.Visiable)
            {
                return;
            }
            if (!string.IsNullOrWhiteSpace(regInfo.Set.Name))
            {
                button.Content = regInfo.Set.Name;
            }
            button.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(Button_Down), true);
            button.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(Button_Up), true);
            button.AddHandler(Button.TouchDownEvent, new RoutedEventHandler(Button_Down), true);
            button.AddHandler(Button.TouchUpEvent, new RoutedEventHandler(Button_Up), true);
        }
        public void Button_Down(object sender, RoutedEventArgs e)
        {
            if(RegisterCmd == null || Type != ButtonVMType.Press)
            {
                return;
            }
            RegisterCmd.Value = true;
        }
        public void Button_Up(object sender, RoutedEventArgs e)
        {
            if (RegisterCmd == null)
            {
                return;
            }
            switch(Type)
            {
                case ButtonVMType.Press:
                    RegisterCmd.Value = false;
                    break;
                case ButtonVMType.Click:
                    if (RegisterInfo != null)
                    {
                        RegisterCmd.Value = !RegisterInfo.Value;
                    }
                    else
                    {
                        RegisterCmd.Value = !RegisterCmd.Value;
                    }
                    break;
            }
        }
    }
}
